#pragma once
#include"Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};
class ServerCal
{
public:
    ServerCal()
    {
    }
    response calhelp(const request&req)
    {
        response res(0, 0);
        switch (req.op)
        {
        case '+':
            res.result = req.x + req.y;
            break;
        case '-':
            res.result = req.x - req.y;
            break;
        case '*':
            res.result = req.x * req.y;
            break;
        case '/':
        {
            if (req.y == 0)
                res.code = Div_Zero;
            else
                res.result = req.x / req.y;
        }
        break;
        case '%':
        {
            if (req.y == 0)
                res.code = Mod_Zero;
            else
                res.result = req.x % req.y;
        }
        break;
        default:
            res.code = Other_Oper;
            break;
        }
        return res;
    }
    string cal(string& package)//计算
    {
        string content;
        bool r=Decode(package,content);
        if(!r) return"";
        request req;
        r=req.DSerialize(content);
        if(!r) return"";


        content="";
        string s2;
        response res=calhelp(req);
        res.Serialize(content);
        s2=Encode(content);
        return s2;
    }
    ~ServerCal()
    {
    }
};